package org.springframework.samples.mvc.employee;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

public class EmployeeValidator implements Validator{

	@Override
	public boolean supports(Class<?> clazz) {

		return EmployeeModel.class.isAssignableFrom(clazz);
	}

	@Override
	public void validate(Object target, Errors errors) {
		EmployeeModel model = (EmployeeModel) target;
		List<Employee> employees = model.getEmployees();
		for(int i=0; i<employees.size(); i++){
			Employee employee = employees.get(i);
			
			if(StringUtils.isEmpty(employee.getEmpNo()))
    			errors.rejectValue("employees["+i+"].empNo", null, "員工代號 不能為空白!"); 
			
			if(StringUtils.isEmpty(employee.getName()))
    			errors.rejectValue("employees["+i+"].name", null, "員工姓名 不能為空白!"); 
			
			//if(employee.getBirthday()==null)
    		//	errors.rejectValue("employees["+i+"].birthday", null, "員工生日 不能為空白!"); 
			
			if(StringUtils.isEmpty(employee.getAddress()))
    			errors.rejectValue("employees["+i+"].address", null, "員工住址 不能為空白!"); 
		}
		
	}

}
